import {searchMusic} from "../../lib/search";
import {audioCtx} from '../../store/constants'
import {getPlay, setPlay, uptPlay} from "../../store/play";
import {getMusicIdx, getPlayList, setMusicIdx, setPlayList} from "../../store/playlist";
import {getUserPlaylist} from "../../store/user";
import {addMusicToListApi} from "../../lib/music";

// const audioCtx = wx.createInnerAudioContext()

Page({
    data: {
        searchInput: '',
        searchResult: [],
        userPlayList: [], // 当前user创建的歌单列表
        musicId: '', // 选择要添加到歌单的music
        showPanel: false // 展开我的歌单列表
    },
    formatTime(time) {
        var minute = Math.floor(time / 60) % 60;
        var second = Math.floor(time) % 60;
        return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    },
    // 添加到歌单
    addToList(e) {
        let lid = e.currentTarget.dataset.lid
        let list = this.data.userPlayList.filter((l) => l.id === lid)[0]
        let that = this
        // 弹窗
        wx.showModal({
            title: '提示',
            content: `是否添加到歌单: ${list.title}`,
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    addMusicToListApi(that.data.musicId, lid,
                        (res) => {
                            console.log(res)
                        })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
        // todo 得到歌单id, 歌曲id, 添加
    },
    setSearchInput(searchInput) {
        this.setData({
            searchInput
        })
    },
    setSearchResult(searchResult) {
        console.log(searchResult)
        this.setData({
            searchResult
        })
    },
    // 输入框改变时修改searchInput数据
    inputSearch(e) {
        this.setSearchInput(e.detail.value)
        console.log(this.data.searchInput)
    },
    // 点击搜索
    onSearch(e) {
        console.log(this.data.searchInput)
        searchMusic(this.data.searchInput,
            (res) => {
                console.log(res)
                this.setSearchResult(res.data.musics)
                // 将当前搜索结果作为播放列表
                setPlayList(res.data.musics)
                console.log(getPlayList())
            })
    },
    // 播放
    playMusic(e) {
        let obj = e.currentTarget.dataset.obj
        audioCtx.src = e.currentTarget.dataset.obj.src
        audioCtx.play()
        setPlay({
            title: obj.title,
            singer: obj.singer,
            coverImgUrl: obj.coverImgUrl,
            currentTime: '00:00',
            duration: '00:00',
            percent: 0
        })
        // console.log(getPlay())
        setMusicIdx(e.currentTarget.dataset.idx)
        console.log(getMusicIdx())
    },
    // 弹出添加到歌单
    starMusic(e) {
        console.log(e.currentTarget.dataset.mid)
        this.setData({
            musicId: e.currentTarget.dataset.mid,
            showPanel: true
        })
        console.log(this.data)
    },

    onReady: function (index) {
        var that = this
        //播放失败检测
        audioCtx.onError(function () {
            console.log('播放失败：' + audioCtx.src)
        })
        //播放结束后自动换下一曲
        audioCtx.onEnded(function () {
            that.next()
        })
        //自动更新播放速度
        audioCtx.onPlay(function () {
        })
        audioCtx.onTimeUpdate(function () {
            uptPlay({
                duration: that.formatTime(audioCtx.duration),
                currentTime: that.formatTime(audioCtx.currentTime),
                percent: audioCtx.currentTime / audioCtx.duration * 100
            })
            console.log(getPlay())
        })
        // 默认选择第一曲
        // this.setMusic(0)
        // 提取全局id后, 可以全局使用, 随着search页改变, 也能获取最新的索引
        this.setMusic(getMusicIdx())
    },


    // 设置当前播放的音乐信息
    setMusic: function () {
        // var music = this.data.playlist[index]
        var music = getPlayList()[getMusicIdx()]
        audioCtx.src = music.src
        setPlay({
            title: music.title,
            singer: music.singer,
            coverImgUrl: music.coverImgUrl,
            currentTime: '00:00',
            duration: '00:00',
            percent: 0
        })
    },


    onLoad: function (options) {
        console.log(getUserPlaylist())
        this.setData({
            userPlayList: getUserPlaylist()
        })
    }
});